/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.PolylineDecoration;
import org.eclipse.draw2d.RotatableDecoration;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;

import edu.pku.sei.modeler.handler.MiddleEndpointLocator;
import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.uml.Dependencies.Dependency;
import edu.pku.sei.modeler.model.uml.Fragments.CombinedFragment;
import edu.pku.sei.modeler.model.uml.Kernel.Comment;

/**
 * @author YanYan
 * 
 */
public class ManifestationEditpart extends UMLLinkEditpart {
	private RotatableDecoration targetDecor;

	private Label nameLabel;

	private boolean isCommentLink = false;

	private boolean isCombinedFragmentDivider = false;

	public ManifestationEditpart(MetaShapeElement model) {
		super(model);

		checkIsCommentLink();

	}

	private void checkIsCommentLink() {
		if (this.getLink().getSource() == null) {
			this.isCommentLink = false;
			this.isCombinedFragmentDivider = false;
			return;
		}

		MetaModelElement sourceModelElement = this.getLink().getSource().getModelElement();
		if (sourceModelElement instanceof Comment) {
			this.isCommentLink = true;
			this.isCombinedFragmentDivider = false;
		} else if (sourceModelElement instanceof CombinedFragment) {
			this.isCommentLink = false;
			this.isCombinedFragmentDivider = true;
		} else {
			this.isCommentLink = false;
			this.isCombinedFragmentDivider = false;
		}

	}

	protected void createEditPolicies() {
		super.createEditPolicies();
	}

	private PolylineConnection getConnection() {
		return (PolylineConnection) getFigure();
	}

	protected IFigure createFigure() {
		PolylineConnection conn = new PolylineConnection();
		conn.setLineStyle(2);

		if (!isCommentLink && !isCombinedFragmentDivider) {
			nameLabel = new Label("<<" + ((Dependency) this.getLink().getModelElement()).getName() + ">>");

			FontData[] fontdata = { new FontData("Arial", 10, SWT.BOLD) };
			nameLabel.setFont(new Font(Display.getCurrent(), fontdata));
			MiddleEndpointLocator relationshipLocator = new MiddleEndpointLocator(conn, new Point(0, 0));

			conn.add(nameLabel, relationshipLocator);
		}

		return conn;
	}

	private PolylineDecoration createDecoration() {
		PolylineDecoration decoration = new PolylineDecoration();
		decoration.setScale(10, 5);
		decoration.setVisible(true);
		//
		if (this.getLink().getSource() != null) {
			MetaModelElement sourceModelElement = this.getLink().getSource().getModelElement();

			if (sourceModelElement instanceof CombinedFragment) {
				decoration.setVisible(false);

			}
		}

		return decoration;
	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		refreshVisuals();
	}

	protected void refreshVisuals() {
		this.checkIsCommentLink();

		if (!isCommentLink && !isCombinedFragmentDivider && this.getLink().getSource() != null) {
			
			if(((Dependency) this.getLink().getModelElement()).getName().length()!=0){				
				nameLabel.setText("<<" + "manifest" + ">>");
			} else {
				nameLabel.setText("");
			}

			updateTargetDecoration();

		}

		super.refreshVisuals();
	}

	private void updateTargetDecoration() {
		if (targetDecor == null) {
			targetDecor = createDecoration();
			getConnection().setTargetDecoration(targetDecor);
		}
	}
}
